/**
 * Created by LiuJie on 2017/10/16.
 */
var express = require("express");
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var util = require('../../util/commonutil');
var router = express.Router();
var resultJSON = require('../common/resultJSON');
router.get("/",function (req, res, next) {
    res.render("dataManage/addAccountData",{id:req.param("id"),titleTxt:req.param("id") ? "修改" : "新增"});
});
router.post("/queryRoleType",function (req, res, next) {
    var jsonResult = {
        total:0,
        rows:[]
    };
    var qs=req.body;
    qs["offset"] = 0;
    qs["limit"] = 100;
    request({
        url:pro.devServeraddr + "heb/employeeRole/query",
        method: "POST",
        qs:qs
    },function (error, response, body) {
        console.log("角色列表数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);

            if(result.success == "true"){
                console.log("rows:"+result.rows.length);
                jsonResult.total = result.total;
                jsonResult.rows = result.rows;
            }else{
                jsonResult.isSuccess=false;
                jsonResult.message="查询数据异常，请稍后重试！"
            }
        }else{
            jsonResult.isSuccess=false;
            jsonResult.message="查询数据异常，请稍后重试！"
        }
        res.json(jsonResult);
    });
    //res.render("dataManage/addAccountData");
});
router.post("/saveData",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysEmp/save",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增账号数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！";
                resultJSON.data = result;
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
router.post("/queryAccountInfo",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysEmp/edit",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("获取账号数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.obj;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="查询数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="查询数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});

module.exports = router;